﻿#region Librerias

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESF.WinForms;
using System.IO;
using ESF.FileProcessing;
using ESF.General;
using System.Diagnostics;
using System.Threading.Tasks;
using ESF.WinForms.Elements;
using ESF.General.GUI;

#endregion


namespace ESF.FileHelper.Controls
{
	public partial class uscMergePDFs : BaseUserControl
	{
		#region Eventos

		public event Program.DelegateReturnMenu ReturningMenu;

		#endregion


		#region Miembros

		private CombinePDFHelper _CombinePDFHelper;

		#endregion


		#region Constructor
		
		public uscMergePDFs() : base ()
		{
			InitializeComponent();

			_CombinePDFHelper = new CombinePDFHelper(ReportProgress);
		}

		#endregion


		#region Funciones

		private void ReportProgress(string pMessage = "", int pTotalWorkCount = 0, int pReadyWorkCount = 0, int pTotalErrors = 0, long pItemElapsedMs = 0)
		{
			GUIHelper.SetAdvance(ProgressDialog, pMessage, pTotalWorkCount, pReadyWorkCount, pTotalErrors, pItemElapsedMs);
		}

		#endregion


		#region Funciones Drop

		private void DropFiles (string [] pListFileNames)
		{
			foreach (string pfileName in pListFileNames.Where (s => Path.GetExtension (s).ToUpper () == ".PDF"))
			{
				ListLoader.AddItem(lstSelectedFiles, pfileName, Path.GetFileName (pfileName));
			}
		}

		#endregion


		#region Button Events

		private void btnReturnMenu_Click(object sender, EventArgs e)
		{
			if (ReturningMenu != null)
			{
				ReturningMenu ();
			}
		}


		private void btnSelectAddFile_Click(object sender, EventArgs e)
		{
			odfOpenFileDialog.CheckFileExists = true;
			odfOpenFileDialog.Title = "Seleccione un archivo PDF para combinar";

			if (odfOpenFileDialog.ShowDialog (this) == DialogResult.OK)
			{
				foreach (string selFile in odfOpenFileDialog.FileNames)
				{
					ListLoader.AddItem (lstSelectedFiles, selFile, Path.GetFileName (selFile));
				}
			}		
		}


		private void btnRemoveFileSelected_Click(object sender, EventArgs e)
		{
			ListLoader.RemoveSelected(lstSelectedFiles);
		}


		private void btnClearSelectedFiles_Click(object sender, EventArgs e)
		{
			ListLoader.RemoveAll(lstSelectedFiles);
		}


		private void btnCombinePDFs_Click(object sender, EventArgs e)
		{
			string tmpFileName;


			lnkGeneratedFile.Visible = false;
			
			if (lstSelectedFiles.Items.Count < 2)
			{
				WinFormsHelper.ShowWarning (this, "Seleccione al menos dos archivos PDF para combinar");
				return;
			}
			
			tmpFileName = Path.GetTempFileName () + ".pdf";
			
			Enabled = false;

			ProgressDialog.HideWhenComplete = false;
			ProgressDialog.Show (ParentForm);
			ProgressDialog.Clear ();

			Task.Factory.StartNew (delegate ()
			{
				try
				{
					_CombinePDFHelper.CombinePDFs (tmpFileName, ListLoader.GetSelectedBasicReferenceCodes (lstSelectedFiles).ToArray ());

					lnkGeneratedFile.Invoke ((Action) delegate () 
					{
						lnkGeneratedFile.Tag = tmpFileName;
						lnkGeneratedFile.Visible = true;
					});
				}
				catch (Exception pEx)
				{
					LogError (pEx);
				}
				finally
				{
					WinFormsHelper.SetEnabled (this);
				}
			});
		}

		#endregion


		#region Link Events

		private void lnkGeneratedFile_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			Process.Start (lnkGeneratedFile.Tag as string);
		}

		#endregion


		#region List Events

		private void lstSelectedFiles_DragDrop(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.None;

			if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				DropFiles(e.Data.GetData(DataFormats.FileDrop) as string[]);
			}
		}


		private void lstSelectedFiles_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Copy;
		}

		#endregion


		#region Control Events

		private void uscMergePDFs_DragEnter(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.Copy;
		}

		private void uscMergePDFs_DragDrop(object sender, DragEventArgs e)
		{
			e.Effect = DragDropEffects.None;

			if (e.Data.GetDataPresent(DataFormats.FileDrop))
			{
				DropFiles(e.Data.GetData(DataFormats.FileDrop) as string[]);
			}
		}

		#endregion
	}
}
